import 'dart:io';
import 'package:flutter/material.dart';
import '/base/logic.dart';
import '/base/view.dart';
import 'package:image_picker/image_picker.dart';
import 'logic.dart';
import 'package:get/get.dart';

class PhotoPage extends StatelessWidget {
  final logic = Get.put(PhotoLogic());
  final state = Get.find<PhotoLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("获取照片"),
      ),
      body: MyBody(),
    );
  }
}

class MyBody extends StatefulWidget {
  @override
  State<MyBody> createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  final ImagePicker _picker = ImagePicker();
  XFile? image;
  var path = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2F7a899e510fb30f24aac12a78c495d143ad4b0376&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650076909&t=f2fe8e944c907677c0b3deec4c3b5c9c";

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextButton(onPressed: () async {
          image = await _picker.pickImage(source: ImageSource.camera);
          if (image != null) {
            path = image!.path;
          }
          setState(() {});
        }, child: Text("拍照")),
        TextButton(
            onPressed: () async {
              image = await _picker.pickImage(source: ImageSource.gallery);
              if (image != null) {
                path = image!.path;
              }
              setState(() {});
            },
            child: Text("相册选择")),
        showImg(path),
      ],
    );
  }

  Widget showImg(String path) {
    if (path.startsWith("http://") || path.startsWith("https://")) {
      return Image.network(path);
    } else {
      return Image.file(File(path));
    }
  }
}
